﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DemoObject;
using BusinessObjects;

public partial class Admin_Controls_MenuTypeAE : System.Web.UI.UserControl
{
    public Guid MenuTypeId { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["MenuTypeId"] != null)
        {
            MenuTypeId = new Guid(Request["MenuTypeId"].ToString());
            btnUpdate.Text = "Cập nhật";
        }
        else MenuTypeId = Guid.Empty;
        if (!IsPostBack)
        {
            LoadData();
        }

    }

    private void LoadData()
    {
        if (!MenuTypeId.Equals(Guid.Empty))
        {
            TblMenuType item = TblMenuTypeRepository.CreateInstant().SelectOne(MenuTypeId);
            txtName.Text = item.Name;
            lblCreatedBy.Text = item.CreatedBy;
            lblCreatedDate.Text = item.CreatedDate.ToString("dd/MM/yyyy");
            lblUpdatedBy.Text = item.UpdatedBy;
            lblUpdatedDate.Text = item.UpdatedDate.ToString("dd/MM/yyyy");
        }
    }
    private TblMenuType GetTblMenuType()
    {
        TblMenuType obj = new TblMenuTypeRepository().SelectOne(MenuTypeId);
        if (obj == null)
        {
            obj = new TblMenuType();
            obj.Id = Guid.NewGuid();
            obj.TextId = new TblMenuTypeRepository().GetUniqueTextIdFromUnicodeText(txtName.Text, 250);
            obj.CreatedBy = HttpContext.Current.User.Identity.Name;
            obj.CreatedDate = DateTime.Now;
        }
        obj.Name = txtName.Text;
        obj.UpdatedBy = HttpContext.Current.User.Identity.Name;
        obj.UpdatedDate = DateTime.Now;

        return obj;
    }


    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (MenuTypeId.Equals(Guid.Empty))
            new TblMenuTypeRepository().Insert(GetTblMenuType());
        else
            new TblMenuTypeRepository().Update(GetTblMenuType());
        Response.Redirect("MenuTypeList.aspx");
    }
}
